<template>
	<view class="jnpf-wrap jnpf-wrap-workflow">
		<u-form :model="info" ref="dataForm" :errorType="['toast']" label-position="left" label-width="150"
			label-align="left">
			<u-form-item label="单别" v-if="judgeShow('tl001')">
				<u-input v-model="info.tl001" placeholder="单别" :disabled="judgeWrite('tl001')"></u-input>
			</u-form-item>
			<u-form-item label="单号" v-if="judgeShow('tl002')">
				<u-input v-model="info.tl002" placeholder="单号" :disabled="judgeWrite('tl002')"></u-input>
			</u-form-item>
			<u-form-item label="单据日期" v-if="judgeShow('tl010')">
				<u-input v-model="info.tl010" placeholder="单据日期" :disabled="judgeWrite('tl010')"></u-input>
			</u-form-item>
			<u-form-item label="币种" v-if="judgeShow('tl005')">
				<u-input v-model="info.tl005" placeholder="币种" disabled></u-input>
			</u-form-item>
			<u-form-item label="供应商编号" v-if="judgeShow('tl004')">
				<u-input v-model="buyTwoName" placeholder="供应商编号" disabled></u-input>
			</u-form-item>
			<u-form-item label="是否含税" v-if="judgeShow('tl008')">
				<u-switch v-model="isShowSwitch" disabled></u-switch>
			</u-form-item>
			<u-form-item label="核价日期" v-if="judgeShow('tl003')">
				<u-input v-model="info.tl003" type="textarea" placeholder="核价日期"
					:disabled="judgeWrite('tl003')"></u-input>
			</u-form-item>
			<u-form-item label="审核码" v-if="judgeShow('tl006')">
				<u-input v-model="info.tl006" type="textarea" placeholder="审核码"
					:disabled="judgeWrite('tl006')"></u-input>
			</u-form-item>
			<u-form-item label="审核者" v-if="judgeShow('tl011')">
				<u-input v-model="info.tl011" type="textarea" placeholder="审核者"
					:disabled="judgeWrite('tl011')"></u-input>
			</u-form-item>
			<u-form-item label="打印次数" v-if="judgeShow('tl009')">
				<u-input v-model="info.tl009" type="textarea" placeholder="打印次数"
					:disabled="judgeWrite('tl009')"></u-input>
			</u-form-item>
			<u-form-item label="备注" v-if="judgeShow('tl007')">
				<u-input v-model="info.tl007" type="textarea" placeholder="请输入备注"
					:disabled="judgeWrite('tl007')"></u-input>
			</u-form-item>

			<u-form-item label="相关附件" v-if="judgeShow('attachmentUpload')">
				<jnpf-file v-model="info.attachmentUpload" :disabled="judgeWrite('attachmentUpload')" />
			</u-form-item>

			<view class="jnpf-table" style="margin-top: 24rpx;">
				<view class="jnpf-table-item" style="margin-bottom: 24rpx;" v-for="(item,i) in lst" :key="i">
					<u-form-item label="序号">
						{{i+1}}
					</u-form-item>
					<u-form-item label="品号" v-if="judgeShow('tm004')">
						<u-input v-model="item.tm004" placeholder="品号" disabled></u-input>
					</u-form-item>
					<u-form-item label="品名" v-if="judgeShow('tm005')">
						<u-input v-model="item.tm005" placeholder="品名"
							:disabled="judgeWrite('tableField113')"></u-input>
					</u-form-item>
					<u-form-item label="规格" v-if="judgeShow('tm006')">
						<u-input v-model="item.tm006" placeholder="规格" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-tm006')
                    "></u-input>
					</u-form-item>
					<u-form-item label="计价单位" v-if="judgeShow('tm009')">
						<u-input v-model="item.tm009" placeholder="计价单位" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-tm009')
                    "></u-input>
					</u-form-item>
					<u-form-item label="参考核价(含税)" v-if="judgeShow('referPrice')">
						<u-input v-model="item.referPrice" placeholder="参考核价(含税)" disabled></u-input>
					</u-form-item>
					<u-form-item label="参考成本(含税)" v-if="judgeShow('ReferenceCost')">
						<u-input v-model="item.ReferenceCost" placeholder="参考成本(含税)" disabled></u-input>
					</u-form-item>
					<u-form-item label="单价" v-if="judgeShow('tm010')">
						<u-input v-model="item.tm010" placeholder="单价" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-tm010')
                    "></u-input>
					</u-form-item>
					<u-form-item label="生效日" v-if="judgeShow('tm014')">
						<u-input v-model="item.tm014" placeholder="请输入生效日" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-tm014')
                    "></u-input>
					</u-form-item>
					<u-form-item label="失效日" v-if="judgeShow('tm015')">
						<u-input v-model="item.tm015" placeholder="请输入失效日" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-tm015')
                    "></u-input>
					</u-form-item>
					<u-form-item label="品号最低价" v-if="judgeShow('udf51')">
						<u-input v-model="item.udf51" placeholder="品号最低价" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-udf51')
                    "></u-input>
					</u-form-item>
					<u-form-item label="此供应商最低核价" labelWidth='250' v-if="judgeShow('udf52')">
						<u-input v-model="item.udf52" placeholder="此供应商最低核价" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-udf52')
                    "></u-input>
					</u-form-item>

					<u-form-item label="标准成本价" v-if="judgeShow('udf54')">
						<u-input v-model="item.udf54" placeholder="请输入标准成本价" :disabled="
                      judgeWrite('tableField113') ||
                      judgeWrite('tableField113-udf54')
                    "></u-input>
					</u-form-item>

				</view>
			</view>
		</u-form>
	</view>
</template>

<script>
	import {
		getPurtlFlowInfo
	} from '@/api/workFlow/flowEngine'
	import {
		getDataInterfaceDataInfo,
		getDataInterfaceRes,
		getDictionaryDataSelector
	} from '@/api/common'
	import comMixin from '../mixin'
	import {
		getGoodsList,
		getCustomerList,
		Info,
		Create,
		Update
	} from '@/api/apply/order'
	export default {
		mixins: [comMixin],
		data() {
			return {
				hide: false,
				billEnCode: 'OrderNumber',
				dataForm: {

				},
				lst: [],
				tl006Options: [],
				info: {},
				formConf: {},
				id: '',
				buyName: '',
				buyTwoName: '',
				isShowSwitch: false,
				payTypeName: '',
			}
		},

		methods: {
			init(data, id, flowFormContentJson) {
				console.log('查询核价单数据id:', id);
				this.setting = data
				this.id = id
				this.getInfo(flowFormContentJson)
			},

			getInfo(data) {

				getPurtlFlowInfo({
					tl001: data.tl001,
					tl002: data.tl002,
				}).then(res => {
					if (res.code == 200) {
						this.info = res.data
						this.lst = res.data.purtmList
						this.isShowSwitch = this.info.tl008 == 'Y' ? true : false
						this.getDictionaryDataSelectorFn()

						this.getDataInterfaceDataInfoMV002(this.info.creator)
						this.getDataInterfaceDataInfoMV003(this.info.tl004)
					}
				})
			},
			getDictionaryDataSelectorFn() {
				getDataInterfaceRes('362115609221143109').then(res => {
					if (res.code == 200) {
						res.data.data.forEach(res => {
							if (res.MF001 == this.info.tl005) {
								this.payTypeName = res.JoinStr
							}
						})
					}
				})
			},
			getDataInterfaceDataInfoMV002(id) {
				let data = {
					id,
					interfaceId: '445425382452126213',
					dblink: this.id,
					propsValue: 'MV001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('445425382452126213', data).then(res => {
					if (res.code == 200) {
						this.buyName = res.data.JoinStr
					}
				})
			},
			getDataInterfaceDataInfoMV003(id) {
				let data = {
					id,
					interfaceId: '381247065990113925',
					dblink: this.id,
					propsValue: 'MA001',
					relationField: 'JoinStr',
				}
				getDataInterfaceDataInfo('381247065990113925', data).then(res => {
					if (res.code == 200) {
						this.buyTwoName = res.data.JoinStr

					}
				})
			},
			gettl006Options(enCode) {
				console.log('enCode', enCode);
				getDictionaryDataSelector("418905791819468293").then((res) => {
					console.log('res', res);
				});
			},
			selfGetInfo() {
				Info(this.setting.id).then(res => {
					this.dataForm = res.data
					if (this.dataForm.fileJson) {
						this.fileList = JSON.parse(this.dataForm.fileJson)
					}
				})
			},
			selfSubmit() {
				this.dataForm.status = this.eventType === 'submit' ? 0 : 1
				if (this.eventType === 'save') return this.selfHandleRequest()
				uni.showModal({
					title: '提示',
					content: '您确定要提交当前流程吗？',
					success: res => {
						if (res.confirm) {
							this.selfHandleRequest()
						}
					}
				})
			},
			selfHandleRequest() {
				if (!this.dataForm.id) delete(this.dataForm.id)
				const formMethod = this.dataForm.id ? Update : Create
				formMethod(this.dataForm).then(res => {
					uni.showToast({
						title: res.msg,
						complete: () => {
							setTimeout(() => {
								uni.$emit('refresh')
								uni.navigateBack()
							}, 1500)
						}
					})
				})
			},
			initData() {
				getGoodsList().then(res => {
					this.goodsOptions = res.data.list
				})
				getCustomerList().then(res => {
					this.customerOptions = res.data.list
				})
			},
			selfInit() {
				this.addGoods()
				this.addPlan()
			},
			onChange(e) {
				const user = e[0]
				this.dataForm.salesmanName = user.fullName
			},
			addGoods() {
				const item = {
					goodsId: '',
					goodsCode: '',
					goodsName: '',
					specifications: '',
					unit: '',
					qty: '',
					price: '',
					amount: '',
					discount: null,
					cess: null,
					actualPrice: '',
					actualAmount: '',
					description: ''
				}
				this.dataForm.goodsList.push(item)
			},
			delGoods(index) {
				this.dataForm.goodsList.splice(index, 1)
			},
			addPlan() {
				const item = {
					receivableDate: '',
					receivableRate: '',
					receivableMoney: '',
					receivableMode: '',
					abstract: ''
				}
				this.dataForm.collectionPlanList.push(item)
			},
			delPlan(index) {
				this.dataForm.collectionPlanList.splice(index, 1)
			},
			onGoodsChange(e, i) {
				const goods = e[0]
				this.dataForm.goodsList[i] = {
					goodsId: goods.id,
					goodsCode: goods.code,
					goodsName: goods.text,
					specifications: goods.specifications,
					unit: goods.unit,
					qty: 1,
					price: goods.price,
					amount: goods.price,
					discount: 100,
					cess: 0,
					actualPrice: goods.price,
					actualAmount: goods.price,
					description: ''
				}
				this.$forceUpdate()
			},
			onCustomerChange(e) {
				const customer = e[0]
				this.dataForm.customerName = customer.text
			},
			count(row) {
				//金额 = 数量*单价
				row.amount = this.jnpf.toDecimal(parseFloat(row.price) * parseFloat(row.qty))
				//折扣价 = (单价*折扣)
				var discountPrice = row.price * (row.discount / 100);
				//实际单价 = 折扣价 * (1 + (税率 / 100))
				row.actualPrice = this.jnpf.toDecimal(discountPrice * (1 + (row.cess / 100)));
				//实际金额
				row.actualAmount = this.jnpf.toDecimal(parseFloat(row.actualPrice) * parseFloat(row
					.qty))
				this.$forceUpdate()
			}
		}
	}
</script>